package com.ruoyi.crm.controller;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.crm.domain.CrmCustomers;
import com.ruoyi.crm.domain.CrmOrders;
import com.ruoyi.crm.mapper.CrmOrdersMapper;
import com.ruoyi.crm.service.ICrmCustomersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 客户Controller
 *
 * @author ruoyi
 * @date 2021-09-23
 */
@RestController
@RequestMapping("/crm/customers")
public class CrmCustomersController extends BaseController
{
    @Autowired
    private ICrmCustomersService crmCustomersService;
    @Resource
    private CrmOrdersMapper crmOrdersService;

    /**
     * 查询客户名称下拉
     * 用于订单管理功能
     */
    @GetMapping("/custName")
    public TableDataInfo findGroupName(CrmCustomers crmCustomers)
    {
        startPage();
        List<CrmCustomers> list = crmCustomersService.selectCrmCustomersFindGroupName(crmCustomers);
        return getDataTable(list);
    }
    /**
     * 查询序号
     * 用于新增时序号自增长
     */
    @GetMapping("/listOrder")
    public TableDataInfo findOrderNum(CrmCustomers crmCustomers)
    {
        startPage();
        List<CrmCustomers> list = crmCustomersService.findOrderNum(crmCustomers);
        return getDataTable(list);
    }

    /**
     * 查询客户列表
     */
    @PreAuthorize(hasPermi = "crm:customers:list")
    @GetMapping("/list")
    public TableDataInfo list(CrmCustomers crmCustomers)
    {
        startPage();
        List<CrmCustomers> list = crmCustomersService.selectCrmCustomersList(crmCustomers);
        return getDataTable(list);
    }

    /**
     * 导出客户列表
     */
    @PreAuthorize(hasPermi = "crm:customers:export")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmCustomers crmCustomers) throws IOException
    {
        List<CrmCustomers> list = crmCustomersService.selectCrmCustomersList(crmCustomers);
        ExcelUtil<CrmCustomers> util = new ExcelUtil<CrmCustomers>(CrmCustomers.class);
        util.exportExcel(response, list, "客户数据");
    }


    /**
     * 获取客户详细信息
     */
    @PreAuthorize(hasPermi = "crm:customers:query")
    @GetMapping(value = "/{custUuid}")
    public AjaxResult getInfo(@PathVariable("custUuid") String custUuid)
    {
        return AjaxResult.success(crmCustomersService.selectCrmCustomersByCustUuid(custUuid));
    }

    /**
     * 新增用户
     */
    @PreAuthorize(hasPermi = "crm:customers:add")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody  CrmCustomers crmCustomers)
    {
        CrmCustomers d = new CrmCustomers();
        d.setDocNum(crmCustomers.getDocNum());
        CrmCustomers c = new CrmCustomers();
        c.setCustName(crmCustomers.getCustName());
        List<CrmCustomers> listByDocNum = crmCustomersService.selectCrmCustomersList(d);
        List<CrmCustomers> listByCustName = crmCustomersService.selectCrmCustomersList(c);
        if(listByCustName.size()>0){
            return new AjaxResult(1521,"您提交的客户名称已存在！请重新填写！");
        }else if(listByDocNum.size()>0){
            return new AjaxResult(1521,"您提交的客户编号已存在！请重新填写！");
        }else {
            int a = crmCustomersService.insertCrmCustomers(crmCustomers);
            return toAjax(a);
        }
    }

    /**
     * 修改客户
     */
    @PreAuthorize(hasPermi = "crm:customers:edit")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CrmCustomers crmCustomers)
    {
            return toAjax(crmCustomersService.updateCrmCustomers(crmCustomers));
    }

    /**
     * 删除客户
     */
    @PreAuthorize(hasPermi = "crm:customers:remove")
    @Log(title = "客户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{custUuids}")
    public AjaxResult remove(@PathVariable String[] custUuids)
    {
        CrmOrders ord = new CrmOrders();
        ord.setFkCustUuids(custUuids);
        List<CrmOrders> lst = crmOrdersService.selectCrmOrdersList(ord);
        if( lst.size() > 0){
            List<String> orderNums = new ArrayList<>();
            for (CrmOrders order:lst
            ) {
                orderNums.add(order.getDocNum());
            }
            // 返回
            return new AjaxResult(1511, "您要删除的客户信息含有相关联的订单数据，暂不能删除！订单编号为："
                    + orderNums.toString() );
        }else {
            return toAjax(crmCustomersService.deleteCrmCustomersByCustUuids(custUuids));
        }


    }
}
